package com.ct.controller.sysapis;

import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.sysapis.model.vo.SysApisVO;
import com.ct.application.sysapis.request.SysApisBatchRequest;
import com.ct.application.sysapis.request.SysApisSaveRequest;
import com.ct.application.sysapis.request.SysApisSearchRequest;
import com.ct.application.sysapis.request.SysApisUpdateRequest;
import com.ct.application.sysapis.service.ISysApisService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.ApiLog;
import com.ct.commons.annotation.HasAdmin;
import com.ct.commons.annotation.PermissionApi;
import com.ct.commons.aspect.limiter.AccessIpLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * RBAC- 权限管理
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@RestController
@PermissionApi(title = "权限管理模块",path="/api/v1/api/",code = "W1000",sorted = 16)
public class SysApisController extends BaseController {

    @Autowired
    private ISysApisService sysApisService;

    /**
     * API列表--租户使用
     * @return
     */
    @PostMapping("/api/system/list")
    @PermissionApi(title = "API列表--租户使用",path="/api/v1/api/system/list",code = "W1001",sorted = 1)
    public List<SysApisVO> findSystemPageMenu() {
        return sysApisService.findSystemPageMenu(2L);
    }

    /**
     * API列表
     *
     * @param apisSearchRequest
     * @return
     */
    @PostMapping("/api/list")
    @PermissionApi(title = "API列表",path="/api/v1/api/list",code = "W1002",sorted = 2)
    public List<SysApisVO> findPageMenu(@RequestBody SysApisSearchRequest apisSearchRequest) {
        return sysApisService.findPageMenu(apisSearchRequest);
    }

    /**
     * 保存后台API
     * @param sysApisSaveRequest
     * @return
     */
    @PostMapping("/api/save")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("保存后台API")
    @PermissionApi(title = "保存后台API",path="/api/v1/api/save",code = "W1003",sorted = 3)
    public SysApisVO saveSysApis(@Validated @RequestBody SysApisSaveRequest sysApisSaveRequest){
        return sysApisService.saveSysApis(sysApisSaveRequest);
    }

    /**
     * 修改后台API
     * @param sysApisSaveRequest
     * @return
     */
    @PostMapping("/api/update")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("修改后台API")
    @PermissionApi(title = "修改后台API",path="/api/v1/api/update",code = "W1004",sorted = 4)
    public SysApisVO updateSysApis(@Validated @RequestBody SysApisUpdateRequest sysApisSaveRequest){
        return sysApisService.updateSysApis(sysApisSaveRequest);
    }

    /**
     * 根据ID查询后台API
     * @param id
     * @return
     */
    @PostMapping("/api/get/{id}")
    @PermissionApi(title = "根据ID查询后台API",path="/api/v1/api/get/{id}",code = "W1005",sorted = 5)
    public SysApisVO getSysApis(@PathVariable("id") Long id){
        return sysApisService.selectById(id);
    }

    /**
     * 删除API
     * @param id
     * @return
     */
    @PostMapping("/api/del/{id}")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("删除API")
    @PermissionApi(title = "删除API",path="/api/v1/api/del/{id}",code = "W1006",sorted = 6)
    public Boolean delSysApis(@PathVariable("id") Long id){
        return sysApisService.deleteById(id);
    }

    /**
     * 根据字段修改状态
     * @param statusRequest
     * @return
     */
    @PostMapping("/api/update/status")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("修改状态")
    @PermissionApi(title = "修改状态",path="/api/v1/api/update/status",code = "W1007",sorted = 7)
    public Boolean updateStatusSysApis(@Validated @RequestBody StatusRequest statusRequest){
        return sysApisService.updateStatusSysApis(statusRequest);
    }

    /**
     * 批量通过
     * @param sysApisBatchRequest
     * @return
     */
    @PostMapping("/api/batch/statuss")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("批量通过")
    @PermissionApi(title = "批量通过",path="/api/v1/api/batch/statuss",code = "W1008",sorted = 8)
    public Boolean batchStatus(@Validated @RequestBody SysApisBatchRequest sysApisBatchRequest){
        return sysApisService.batchStatus(sysApisBatchRequest);
    }

    /**
     * 批量禁止
     * @param sysApisBatchRequest
     * @return
     */
    @PostMapping("/api/batch/forbiddens")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("批量禁止")
    @PermissionApi(title = "批量禁止",path="/api/v1/api/batch/forbiddens",code = "W1009",sorted = 9)
    public Boolean batchForbiddens(@Validated @RequestBody SysApisBatchRequest sysApisBatchRequest){
        return sysApisService.batchForbiddens(sysApisBatchRequest);
    }

}
